// Copyright (C) 2012 by Johann Duscher <jonny.dee@gmx.net>, Kentaro Fukuchi <kentaro@fukuchi.org>
//
// This file is part of barcodeart-pp.
//
// barcodeart-pp is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// barcodeart-pp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with barcodeart-pp. If not, see <http://www.gnu.org/licenses/>.

#include <cassert>

#include <barcodeart/IBarCode.hpp>

#include "barcodeart/epswriter/EPSWriter.hpp"
#include "barcodeart/epswriter/EPSWriterConfig.hpp"


namespace barcodeart
{

namespace epswriter
{

int EPSWriter::write(std::ostream& out, const IBarCode& barCode, const IWriterConfig& iConfig) const
{
    const EPSWriterConfig& config = dynamic_cast<const EPSWriterConfig&>(iConfig);

    /* EPS file header */
    out << "%%!PS-Adobe-2.0 EPSF-1.2\n"
             "%%%%BoundingBox: 0 0 " << barCode.getWidth()*config.getPixelWidth() << " " << barCode.getHeight()*config.getPixelHeight() << "\n"
             "%%%%Pages: 1 1\n"
             "%%%%EndComments\n";
    /* draw point */
    out << "/p { "
             "moveto "
             "0 1 rlineto "
             "1 0 rlineto "
             "0 -1 rlineto "
             "fill "
           "} bind def " << config.getPixelWidth() << " " << config.getPixelHeight() << " scale ";

    /* data */
    for(int y = 0; y < barCode.getHeight(); y++)
    {
        int py = (barCode.getHeight() - y - 1);

        for(int x = 0; x < barCode.getWidth(); x++)
        {
            int px = x;

            if(barCode.isBlack(y, x))
                out << px << " " << py << " p ";
        }
    }

    out << "\n%%%%EOF\n";

    return 0;
}

}

}
